<!-- login -->
<template>
  <div class="container b-container" id="b-container" ref="bContainer">
    <form class="form" id="b-form" method="" action="">
      <h2 class="form_title title">登录网站</h2>
      <div class="form__icons">
        <span class="form__icon iconfont icon-chatgpt"></span>
        <span class="form__icon iconfont icon-github"></span>
        <span class="form__icon iconfont icon-deepseek"></span>
      </div>
      <span class="form__span">请使用邮箱进行登录</span>
      <input class="form__input" :class="{ 'input-error': emailError }" type="text" placeholder="邮箱" v-model="email"
        @blur="validateEmail" @focus="resetEmailError" />
      <input class="form__input" :class="{ 'input-error': passwordError }" type="password" placeholder="请输入密码"
        v-model="password" @blur="validatePassword" @focus="resetPasswordError" /><a class="form__link">忘记密码?</a>
      <button class="form__button button submit" type="button" @click="login">
        登录
      </button>
    </form>
  </div>
</template>

<script setup lang="js">
  import { useAuthFormsStore } from '@/store/authFormsStore';
  import { ref, watch, onMounted } from 'vue';
  import { storeToRefs } from 'pinia';
  import { ElMessage } from 'element-plus';
  import { loginAPI } from '@/api';
  import { useRouter } from 'vue-router';

  const authFormsStore = useAuthFormsStore();
  const { showSignup } = storeToRefs(authFormsStore);
  const router = useRouter();

  const email = ref('');
  const password = ref('');
  const emailError = ref(false);
  const passwordError = ref(false);
  const bContainer = ref(null);

  const validateEmail = () => {
    const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    emailError.value = !emailPattern.test(email.value);
  };

  const validatePassword = () => {
    passwordError.value = !password.value;
  };

  const resetEmailError = () => {
    emailError.value = false;
  };

  const resetPasswordError = () => {
    passwordError.value = false;
  };

  const login = async () => {
    validateEmail();
    validatePassword();

    if (emailError.value || passwordError.value) {
      ElMessage.warning('请正确填写邮箱和密码');
      return;
    }

    try {
      await loginAPI(email.value, password.value, router);
    } catch (error) {
      console.error('登录失败', error);
      ElMessage.error('登录失败');
    }
  };

  onMounted(() => {
    watch(showSignup, () => {
      bContainer.value.classList.toggle('is-txl');
      bContainer.value.classList.toggle('is-z200');
    });
  });
</script>

<style scoped>
  *,
  *::after,
  *::before {
    box-sizing: border-box;
    user-select: none;
  }

  .container {
    display: flex;
    justify-content: center;
    align-items: center;
    position: absolute;
    top: 0;
    width: 600px;
    height: 100%;
    padding: 25px;
    background-color: #ffffff;
    transition: 1.25s;
  }

  .b-container {
    left: calc(100% - 600px);
    z-index: 0;
  }

  .form {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    width: 100%;
    height: 100%;
  }

  .form__icon {
    object-fit: contain;
    width: 30px;
    margin: 0 5px;
    opacity: 0.5;
    transition: 0.15s;
  }

  .form__icon:hover {
    opacity: 1;
    transition: 0.15s;
    cursor: pointer;
  }

  .form__span {
    margin-top: 30px;
    margin-bottom: 12px;
  }

  .form__input {
    width: 350px;
    height: 40px;
    margin: 4px 0;
    padding-left: 25px;
    font-size: 13px;
    letter-spacing: 0.15px;
    border: none;
    outline: none;
    font-family: "Montserrat", sans-serif;
    background-color: #fefefe;
    transition: 0.25s ease;
    border-radius: 8px;
    box-shadow: inset 2px 2px 4px #d1d9e6, inset -2px -2px 4px #f9f9f9;
  }

  .form__input:focus {
    box-shadow: inset 4px 4px 4px #d1d9e6, inset -4px -4px 4px #f9f9f9;
  }

  .form__link {
    color: #181818;
    font-size: 15px;
    margin-top: 25px;
    border-bottom: 1px solid #a0a5a8;
    line-height: 2;
  }

  .form__button {
    cursor: pointer;
  }

  .form__button:hover {
    box-shadow: 6px 6px 10px #d1d9e6, -6px -6px 10px #f9f9f9;
    transform: scale(0.985);
    transition: 0.25s;
  }

  .form__button:active,
  .form__button:focus {
    box-shadow: 2px 2px 6px #d1d9e6, -2px -2px 6px #f9f9f9;
    transform: scale(0.97);
    transition: 0.25s;
  }

  .button {
    width: 180px;
    height: 50px;
    border-radius: 25px;
    margin-top: 50px;
    font-weight: 700;
    font-size: 14px;
    letter-spacing: 1.15px;
    background-color: #4b70e2;
    color: #f9f9f9;
    box-shadow: 8px 8px 16px #d1d9e6, -8px -8px 16px #f9f9f9;
    border: none;
    outline: none;
  }

  .title {
    font-size: 34px;
    font-weight: 700;
    line-height: 3;
    color: #181818;
  }

  .is-txl {
    left: 0;
    transition: 1.25s;
    transform-origin: right;
  }

  .is-z200 {
    z-index: 200;
    transition: 1.25s;
  }

  .input-error {
    border: 1px solid red;
  }
</style>